<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <base href="<%=request.getContextPath()%>/">
    <meta charset="UTF-8">
    <title>用户管理 - 医疗预约系统后台</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link rel="stylesheet" href="statics/css/hisStyle.css">
    <link rel="stylesheet" href="statics/css/common-styles.css">
</head>
<body>
<%@include file="../common/sidebar.jsp"%>
<div class="main-content">
    <%@include file="../common/top.jsp"%>
    <div class="function-page">
        <div class="page-header">
            <h1>用户管理</h1>
        </div>
        
        <div class="action-bar">
            <a href="user/toAdd" class="btn btn-primary">
                <i class="fas fa-plus"></i> 新增用户
            </a>
        </div>

        <!-- 查询表单 -->
        <div class="search-filter">
            <form id="userFindForm" action="user/find" method="get" class="search-form">
                <div class="form-group">
                    <label for="username">用户名：</label>
                    <input type="text" id="username" name="username" value="${param.username}" class="form-control"/>
                </div>
                <div class="form-group">
                    <label for="email">邮箱：</label>
                    <input type="text" id="email" name="email" value="${param.email}" class="form-control"/>
                </div>
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-search"></i> 查询
                </button>
            </form>
        </div>

        <div class="data-section">
            <table class="data-table">
                <thead>
                <tr>
                    <th>用户名</th>
                    <th>邮箱</th>
                    <th>手机号</th>
                    <th>用户类型</th>
                    <th>创建时间</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${pageInfo.list}" var="user">
                    <tr>
                        <td>${user.username}</td>
                        <td>${user.email}</td>
                        <td>${user.phone}</td>
                        <td>${user.userType eq 1 ? '管理员' : '普通用户'}</td>
                        <td>${user.createTime}</td>
                        <td><span class="status-badge status-active">正常</span></td>
                        <td class="operations">
                            <button class="btn btn-primary btn-sm" onclick="findById(${user.userId})"><i class="fas fa-edit"></i> 编辑</button>
                            <button class="btn btn-danger btn-sm" onclick="removeById(${user.userId})"><i class="fas fa-trash"></i> 删除</button>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
        
        <div class="data-section">
            <div class="pagination-container">
                <div class="pagination-info">
                    总计 ${pageInfo.total} 条记录，当前第 ${pageInfo.pageNum} / ${pageInfo.pages} 页
                </div>
                <div class="pagination">
                    <a href="javascript:goToUrl(1)" class="pagination-link" ${pageInfo.pageNum == 1 ? 'disabled' : ''}>首页</a>
                    <a href="javascript:goToUrl(${pageInfo.pageNum-1})" class="pagination-link" ${pageInfo.pageNum == 1 ? 'disabled' : ''}>上一页</a>
                    <span class="pagination-info">每页 ${pageInfo.pageSize} 条</span>
                    <a href="javascript:goToUrl(${pageInfo.pageNum+1})" class="pagination-link" ${pageInfo.pageNum == pageInfo.pages ? 'disabled' : ''}>下一页</a>
                    <a href="javascript:goToUrl(${pageInfo.pages})" class="pagination-link" ${pageInfo.pageNum == pageInfo.pages ? 'disabled' : ''}>尾页</a>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    // 分页跳转函数
    function goToUrl(pageNum) {
        if (pageNum < 1) pageNum = 1;
        if (pageNum > ${pageInfo.pages}) pageNum = ${pageInfo.pages};

        const userName = document.querySelector('input[name="username"]').value;
        const email = document.querySelector('input[name="email"]').value;

        let url = 'user/find?pageNum=' + pageNum;
        if (userName) url += '&username=' + encodeURIComponent(userName);
        if (email) url += '&email=' + encodeURIComponent(email);

        window.location.href = url;
    }

    function findById(userId) {
        window.location.href = "user/findById?userId=" + userId;
    }

    function removeById(userId) {
        if (confirm("确定删除该用户吗？")) {
            window.location.href = "user/removeById?userId=" + userId;
        }
    }
</script>
</body>
</html>